package com.csi.okr.mobile.department.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.csi.okr.common.util.BasicValueUtil;
import com.csi.okr.common.util.WxUtil;
import com.csi.okr.mobile.common.task.DataTask;
import com.csi.okr.mobile.company.service.CompanyService;
import com.csi.okr.mobile.department.service.DeptService;

@Controller
@RequestMapping("/org")
public class DeptController {

	@Resource
	private DeptService deptService;
	@Resource
	private CompanyService compService;
	
	
	/**
	 * 微信请求入口 - 当前
	 * @return
	 */
    @RequestMapping(value = "/oauth")
    public ModelAndView oauth() {
    	//回调地址
    	String redirect_url = WxUtil.DOMAIN + "/org/callback";
    	//授权地址
    	StringBuilder url = new StringBuilder();
    	url.append("https://open.weixin.qq.com/connect/oauth2/authorize");
    	url.append("?appid="+WxUtil.CORPID);
    	url.append("&redirect_uri="+redirect_url);
    	url.append("&response_type=code");
    	url.append("&scope=snsapi_base");
    	url.append("&state=oauth");
    	url.append("#wechat_redirect");
        ModelAndView mv = new ModelAndView();
        mv.setView(new RedirectView(url.toString()));
        return mv;
    }
    
    /**
     * 微信请求入口 - 查询
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {
    	//回调地址
    	String redirect_url = WxUtil.DOMAIN + "/org/callback";
    	//授权地址
    	StringBuilder url = new StringBuilder();
    	url.append("https://open.weixin.qq.com/connect/oauth2/authorize");
    	url.append("?appid="+WxUtil.CORPID);
    	url.append("&redirect_uri="+redirect_url);
    	url.append("&response_type=code");
    	url.append("&scope=snsapi_base");
    	url.append("&state=query");
    	url.append("#wechat_redirect");
    	ModelAndView mv = new ModelAndView();
    	mv.setView(new RedirectView(url.toString()));
    	return mv;
    }
    
    /**
     * 微信回调地址
     * @param code 微信返回参数 <br>
     * @param session
     * @return
     */
    @RequestMapping(value = "/callback")
    public ModelAndView callback(@RequestParam String code,String state,HttpSession session) {
    	
    	String emplCode = WxUtil.getEmplCode(code);
    	
    	if(emplCode == null)
    		return oauth();
    	
    	if (emplCode.equals("index") || emplCode.equals("lixun") || emplCode.equals("E100021495")) {
    		emplCode = "E000100834";
		} 
    	
    	ModelAndView mv = new ModelAndView();
		mv.setViewName("org/orgokr");
		mv.addObject("emplCode",emplCode);
		mv.addObject("param", state);
		String orgCode = compService.getDefaultOrgCode(emplCode).toString();
		mv.addObject("orgCode", orgCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
		
		session.setAttribute("tree", deptService.getOrgTree(emplCode));
		session.setAttribute("years", BasicValueUtil.getDate2Now());
		session.setAttribute("oColor", DataTask.OCOLOR);
		session.setAttribute("krColor", DataTask.KRCOLOR);
		session.setAttribute("oState", DataTask.O_STATE);
		session.setAttribute("oTeam", DataTask.O_TEAM);
		session.setAttribute("rState", DataTask.R_STATE);
		session.setAttribute("sState", DataTask.S_STATE);
		return mv;
    }
    
    @RequestMapping(value = "/index")
    public ModelAndView index(@RequestParam String code,String param,HttpSession session) {
    	
    	ModelAndView mv = new ModelAndView();
    	mv.setViewName("org/orgokr");
    	mv.addObject("emplCode",code);
    	mv.addObject("param",param);
    	String orgCode = compService.getDefaultOrgCode(code).toString();
		mv.addObject("orgCode", orgCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
    	
    	session.setAttribute("tree", deptService.getOrgTree(code));
    	session.setAttribute("oColor", DataTask.OCOLOR);
    	session.setAttribute("krColor", DataTask.KRCOLOR);
    	session.setAttribute("oState", DataTask.O_STATE);
    	session.setAttribute("oTeam", DataTask.O_TEAM);
    	session.setAttribute("rState", DataTask.R_STATE);
    	session.setAttribute("sState", DataTask.S_STATE);
    	return mv;
    }

    /**
     * 组织树点击跳转方法
     * @param emplCode
     * @param orgCode
     * @return
     */
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public ModelAndView tree(@RequestParam String emplCode,String orgCode,HttpSession session) {
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("org/orgokr");
		mv.addObject("emplCode",emplCode);
		mv.addObject("orgCode",orgCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
		return mv;
	}

	/**
	 * 右侧部门成员
	 * 
	 * @param orgCode
	 * @return
	 */
	@RequestMapping(value = "/subset", method = RequestMethod.POST)
	@ResponseBody
	public Object subset(@RequestParam String emplCode,String orgCode,String year,String month,String quarter,HttpSession session) {

		return deptService.getSubsetOrg(orgCode, emplCode, year, month, quarter);
	}
	
	/**
	 * 跳转成员OKR
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/member", method = RequestMethod.GET)
	public ModelAndView member(@RequestParam String emplCode,String memberCode,String orgCode) {

		ModelAndView mv = new ModelAndView();
		mv.addObject("orgCode",orgCode);
		mv.addObject("memberCode",memberCode);
		mv.addObject("emplCode",emplCode);
		mv.addObject("tree", deptService.getOrgTree(emplCode));
		mv.setViewName("org/emplokr");
		return mv;
	}
	
	/**
	 * 查询成员OKR可见数据
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/okr", method = RequestMethod.POST)
	@ResponseBody
	public Object okr(@RequestParam int pageIndex,int pageSize,String emplCode,String memberCode,String orgCode, String month, String year, String quarter,
			String state, String odefinition) {

		return deptService.getOkrByMemberCode(pageIndex,pageSize,orgCode,emplCode,memberCode,
				month, year, quarter, state, odefinition);
	}
}
